package cn.edu.scau.cmi.luodongmei.hibernate.dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.edu.scau.cmi.luodongmei.hibernate.domain.Teacher;
import cn.edu.scau.cmi.luodongmei.hibernate.util.HibernateUtils;

public class TeacherDAO {
	private static final Logger log = LoggerFactory.getLogger(TeacherDAO.class);

	public boolean save(Teacher teacher) {
		log.debug("saving Teacher instance");
		//System.out.println("saving Teacher instance");
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			Transaction transaction = session.beginTransaction();
			session.save(teacher);
			log.debug("save successful");
			//System.out.println("save successful");
			transaction.commit();
			return true;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			//System.out.println("save failed");
			return false;
		} finally {
			if(session!=null)
				session.close();
		}
	}

	public boolean delete(Teacher teacher) {
		log.debug("deleting Teacher instance");
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			Transaction transaction = session.beginTransaction();
			session.delete(teacher);
			log.debug("delete successful");
			transaction.commit();
			return true;
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			return false;
		} finally {
			if(session!=null)
				session.close();
		}
	}
	
	public boolean update(Teacher teacher) {
		log.debug("updating Teacher instance");
		//System.out.println("updating Teacher instance");
		Session session = null;
		try {
			session = HibernateUtils.getSession();
			Transaction transaction = session.beginTransaction();
			session.update(teacher);
			log.debug("update successful");
			//System.out.println("update successful");
			transaction.commit();
			return true;
		} catch (RuntimeException re) {
			log.error("update failed",re);
			//System.out.println("update failed");
			return false;
		} finally {
			if(session!=null)
				session.close();
		}
	}
	
	public Teacher findByName(String name) {
		log.debug("finding Teacher by name, value: " + name);
		Session s = null;
		try {
			s = HibernateUtils.getSession();
			String queryString = "from Teacher as teacher where teacher.name= ?";
			Query queryObject = s.createQuery(queryString);
			queryObject.setParameter(0, name);
			Teacher Teacher = (Teacher) queryObject.uniqueResult();
			return Teacher;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		} finally {
			if(s!=null)
				s.close();
		}

	}

	public List findAll() {
		log.debug("finding all Teacher instances");
		Session s = null;
		try {
			String queryString = "from Teacher";
			s = HibernateUtils.getSession();
			Query queryObject = s.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		} finally {
			if(s!=null)
				s.close();
		}
	}
}